package com.yiren.backstage.communication.analysis;

import android.content.Context;

import com.yiren.backstage.communication.services.IAnalysisStateService;
import com.yiren.backstage.communication.services.impl.AnalysisStateServiceImpl;
import com.yiren.backstage.communication.utils.BytesUtils;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.enums.GearTypeEnum;
import com.yiren.backstage.config.enums.SpeedTypeEnum;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.backstage.currency.enums.CarStateValueEnum;

import java.util.Map;

/**
 * Time:2021/12/03 9:34
 * Author:xiaozan
 * Description:免破数据帧解析
 */
public class MpDataAnalysis {

    private Context context;
    private Map<String, Object> carSignalMap;
    private IAnalysisStateService iAnalysisStateService;
    public CarStateBean carState = CarStateBean.getInstance();  //车辆状态

    public MpDataAnalysis(Context context) {
        this.context = context;
        iAnalysisStateService = new AnalysisStateServiceImpl(context);
    }

    public void dataAnalysis(String data) {
        // 防止数据解析异常给的try,catch
        try {
            String[] carDataArray = data.split(";");
            if (!data.contains("MP") || carDataArray.length < 11) {
                return;
            }
            carSignalMap = LoadConfigApi.loadCarSignalConfig(context).getMap();
            //obd车型
            carState.setObdCarModel(carDataArray[1]);

            //车速
            if (LoadConfigApi.loadSignalSourceConfig(context).getSpeedType() == SpeedTypeEnum.OBD) {
                //北汽绅宝D50车速补偿
                if (LoadConfigApi.loadSignalSourceConfig(context).getObdModel() == 8) {
                    carState.setSpeed(Math.round(Float.parseFloat(carDataArray[2]) + (Float.parseFloat(carDataArray[2]) * 0.28)));
                } else {
                    carState.setSpeed(Float.parseFloat(carDataArray[2]));
                }
            }

            //转速
            carState.setRotateSpeed(Float.parseFloat(carDataArray[3]));

            //挡位  安装空挡检测器才解析OBD挡位
            if (LoadConfigApi.loadSignalSourceConfig(context).getGearType() == GearTypeEnum.NULL_GEAR) {
                carState.setGear(iAnalysisStateService.obdGear(Integer.parseInt(carDataArray[4])));
            }

            //方向盘角度
            float steerangel = Float.parseFloat(carDataArray[5]);
            if (steerangel > 0) {
                carState.setSteerAngel(((steerangel - 32768) / 10));
            }

            //开关信号量1：离合器信号、安全带信号、主驾车门信号、手刹信号、脚刹信号、喇叭信号、车钥匙状态
            byte[] bytes6 = BytesUtils.int2bytes(Integer.parseInt(carDataArray[6]));

            String jxCarStateConfig = LoadConfigApi.loadDeviceStudyConfig(context).getJxConfig();
            // 离合
            if (!jxCarStateConfig.contains(CarStateNameEnum.SEPARATION_REUNION.toString())) {
                carState.setSeparationReunion(iAnalysisStateService.brakeingAndLoosenState(CarStateNameEnum.SEPARATION_REUNION, (Boolean) carSignalMap.get(CarStateNameEnum.SEPARATION_REUNION.toString()), bytes6[0]));
            }

            // 安全带
            if (!jxCarStateConfig.contains(CarStateNameEnum.SAFETY_BELT.toString())) {
                carState.setSafetyBelt(iAnalysisStateService.hitchAndLoosenState(CarStateNameEnum.SAFETY_BELT, (Boolean) carSignalMap.get(CarStateNameEnum.SAFETY_BELT.toString()), bytes6[1]));
            }

            // 车门:0表示不检测，1表示开启，2表示关闭
            if (!jxCarStateConfig.contains(CarStateNameEnum.CAR_DOOR.toString())) {
                carState.setCarDoor(iAnalysisStateService.openAndCloseState(CarStateNameEnum.CAR_DOOR, (Boolean) carSignalMap.get(CarStateNameEnum.CAR_DOOR.toString()), bytes6[2]));
            }

            // 手刹:0表示不检测，1表示拉紧,2表示松开
            if (!jxCarStateConfig.contains(CarStateNameEnum.PARKING_BRAKE.toString())) {
                carState.setParkingBrake(iAnalysisStateService.loosenAndCockingState(CarStateNameEnum.PARKING_BRAKE, (Boolean) carSignalMap.get(CarStateNameEnum.PARKING_BRAKE.toString()), bytes6[3]));
            }

            // 脚刹:0表示不检测，1表示刹车,2表示松开
            if (!jxCarStateConfig.contains(CarStateNameEnum.FOOT_BRAKE.toString())) {
                carState.setFootBrake(iAnalysisStateService.brakeingAndLoosenState(CarStateNameEnum.FOOT_BRAKE, (Boolean) carSignalMap.get(CarStateNameEnum.FOOT_BRAKE.toString()), bytes6[4]));
            }

            // 喇叭 :0表示不检测，1表示开启,2表示关闭
            if (!jxCarStateConfig.contains(CarStateNameEnum.HORN.toString())) {
                carState.setHorn(iAnalysisStateService.openAndCloseState(CarStateNameEnum.HORN, (Boolean) carSignalMap.get(CarStateNameEnum.HORN.toString()), bytes6[5]));
            }

            // 钥匙 :00表示LOCK/OFF，01表示ACC,10表示START,11表示ON
            carState.setCarKey(iAnalysisStateService.carKeyState(bytes6));

            //开关信号量2:远光灯信号、近光灯信号、后雾灯信号、示宽灯信号、警示灯信号、左转向灯信号、右转向灯信号、前雾灯信号
            byte[] bytes7 = BytesUtils.int2bytes(Integer.parseInt(carDataArray[7]));
            // 远光
            if (!jxCarStateConfig.contains(CarStateNameEnum.DISTANCE_LIGHT.toString())) {
                carState.setDistanceLight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.DISTANCE_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.DISTANCE_LIGHT.toString()), bytes7[0]));
            }

            // 近光
            if (!jxCarStateConfig.contains(CarStateNameEnum.PASSING_LIGHT.toString())) {
                carState.setPassingLight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.PASSING_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.PASSING_LIGHT.toString()), bytes7[1]));
            }

            // 雾灯
            if (!jxCarStateConfig.contains(CarStateNameEnum.FOGLIGHT.toString())) {
                carState.setFoglight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.FOGLIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.FOGLIGHT.toString()), bytes7[2]));
            }

            // 行车灯
            if (!jxCarStateConfig.contains(CarStateNameEnum.DRIVING_LAMP.toString())) {
                carState.setDrivingLamp(iAnalysisStateService.openAndCloseState(CarStateNameEnum.DRIVING_LAMP, (Boolean) carSignalMap.get(CarStateNameEnum.DRIVING_LAMP.toString()), bytes7[3]));
            }

            //雨刮器
            if (!jxCarStateConfig.contains(CarStateNameEnum.RAIN_WIPPER.toString())) {
                carState.setRainWipper(iAnalysisStateService.corneringLamp(CarStateNameEnum.RAIN_WIPPER, (Boolean) carSignalMap.get(CarStateNameEnum.RAIN_WIPPER.toString()), bytes7[4]));
            }

            // 左转灯
            if (!jxCarStateConfig.contains(CarStateNameEnum.LEFT_LIGHT.toString())) {
                carState.setLeftLight(iAnalysisStateService.corneringLamp(CarStateNameEnum.LEFT_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.LEFT_LIGHT.toString()), bytes7[5]));
            }

            // 右转灯
            if (!jxCarStateConfig.contains(CarStateNameEnum.RIGHT_LIGHT.toString())) {
                carState.setRightLight(iAnalysisStateService.corneringLamp(CarStateNameEnum.RIGHT_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.RIGHT_LIGHT.toString()), bytes7[6]));
            }

            // 警示灯
            if (!jxCarStateConfig.contains(CarStateNameEnum.LEFT_LIGHT.toString()) && !jxCarStateConfig.contains(CarStateNameEnum.RIGHT_LIGHT.toString())) {
                if (carState.getLeftLight() == CarStateValueEnum.OPEN && carState.getRightLight() == CarStateValueEnum.OPEN) {
                    carState.setCautionLight(CarStateValueEnum.OPEN);
                } else {
                    carState.setCautionLight(CarStateValueEnum.CLOSE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
